
package de.justphil.tcg.tcgandroidclient.content.view;

import java.util.List;

import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.os.Handler;
import de.justphil.tcg.tcgandroidclient.content.TCGContentAdapter;
import de.justphil.tcg.tcgandroidclient.content.sqlite.SqliteCard;
import de.justphil.tcg.tcgandroidclient.util.StringTools;
import de.justphil.tcg.tcgandroidclient.util.TCGLog;

/**
 * @author Martin
 * 
 */
public class ViewCard extends ViewData {
	
	private final TCGContentAdapter contentAdapter;
	private final long cardId;
	/* */
	private int type;
	// private ContentObserver typeObserver;
	/* */
	private String name;
	private int life;
	private int attack;
	private int defense;
	private int agility;
	private boolean cardSet;
	// private ContentObserver cardObserver;
	/* */
	private Bitmap image;
	private ContentObserver imageObserver;
	/* */
	private List<ViewProperty> properties;
	private ContentObserver propertiesObserver;
	
	public ViewCard(TCGContentAdapter contentAdapter, ContentObserver obs, long cardId) {
	
		super(obs);
		this.contentAdapter = contentAdapter;
		this.cardId = cardId;
		TCGLog.d(new Object[] {
				"Created card ", getCardId(),"." });
	}
	
	public ViewCard(TCGContentAdapter contentAdapter, ContentObserver obs, long cardId, int type) {
	
		this(contentAdapter, obs, cardId);
		this.type = type;
	}
	
	@Override
	public String toString() {
	
		return StringTools.getString(new Object[] {
				getName(), " (", getCardId(), ")" });
	}
	
	public long getCardId() {
	
		return cardId;
	}
	
	public int getType() {
	
		if (type == 0) {
			setType(contentAdapter.readCardInstance(getCardId(), getContentObserver()));
		}
		// resetNotification();
		return type;
	}
	
	public String getName() {
	
		int type = getType();
		if (!cardSet && type != 0) {
			setCard(contentAdapter.readCard(type, getContentObserver()));
		}
		// resetNotification();
		return name;
	}
	
	public int getLife() {
	
		int type = getType();
		if (!cardSet && type != 0) {
			setCard(contentAdapter.readCard(type, getContentObserver()));
		}
		// resetNotification();
		return life;
	}
	
	public int getAttack() {
	
		int type = getType();
		if (!cardSet && type != 0) {
			setCard(contentAdapter.readCard(type, getContentObserver()));
		}
		// resetNotification();
		return attack;
	}
	
	public int getDefense() {
	
		int type = getType();
		if (!cardSet && type != 0) {
			setCard(contentAdapter.readCard(type, getContentObserver()));
		}
		// resetNotification();
		return defense;
	}
	
	public int getAgility() {
	
		int type = getType();
		if (!cardSet && type != 0) {
			setCard(contentAdapter.readCard(type, getContentObserver()));
		}
		// resetNotification();
		return agility;
	}
	
	public Bitmap getImage() {
	
		int type = getType();
		if (image == null && type != 0) {
			setImage(contentAdapter.readCardImage(type, getImageObserver()));
		}
		// resetNotification();
		return image;
	}
	
	public List<ViewProperty> getProperties() {
	
		// TODO
		return null;
	}
	
	private void setType(int type) {
	
		TCGLog.e(new Object[] {
				"Setting type to ", type });
		this.type = type;
		// onChange();
	}
	
	private void setImage(Bitmap image) {
	
		this.image = image;
		// onChange();
	}
	
	private void setCard(SqliteCard card) {
	
		cardSet = true;
		String name = card.getName();
		if (name == null || "".equals(name) || "NULL".equals(name)) {
			name = "unknown";
		}
		this.name = name;
		this.life = card.getLife();
		this.attack = card.getAttack();
		this.defense = card.getDefense();
		this.agility = card.getAgility();
		// onChange();
	}
	
	private ContentObserver getImageObserver() {
	
		if (imageObserver == null) {
			imageObserver = new ContentObserver(new Handler()) {
				
				@Override
				public void onChange(boolean selfChange) {
				
					setImage(contentAdapter.readCardImage(getType(), null));
				}
			};
		}
		return imageObserver;
	}
	// private ContentObserver getCardObserver() {
	//
	// if (cardObserver == null) {
	// cardObserver = new ContentObserver(new Handler()) {
	//
	// @Override
	// public void onChange(boolean selfChange) {
	//
	// setCard(contentAdapter.readCard(getType(), null));
	// }
	// };
	// }
	// return cardObserver;
	// }
	//
	// private ContentObserver getTypeObserver() {
	//
	// if (typeObserver == null) {
	// typeObserver = new ContentObserver(new Handler()) {
	//
	// @Override
	// public void onChange(boolean selfChange) {
	//
	// setType(contentAdapter.readCardInstance(getCardId(), null));
	// }
	// };
	// }
	// return typeObserver;
	// }
}
